$(document).ready(
		function() {
			$("form").submit(function() {
				return false;
			});
			var VAL = {
				NORMAL : "0000000",
				/*EVT_USER : $("#userId").text(),*/
				EVT_USER : "C1702003",
				T_FDPAPLYWO : "FDPAPLYWO",
				T_FBPBISOPE : "FBPBISOPE",
				T_FSPWOCHG : "FSPWOCHG",
				T_FBPBISPTH :"FBPBISPTH",
				DISABLED_ATTR : {
					"disabled" : true
				},
				ENABLED_ATTR : {
					"disabled" : false
				}
			};
			var domObj = {
				$window : $(window),

				$woIdFkA : $("#woIdFkA"),
				$opeId : $("#opeId"),
				$boxId : $("#boxId"),
				$woIdFkB : $("#woIdFkB"),

				grids : {
					$boxInfoGrd : $("#boxInfoGrd")
				},

				buttons : {
					$query_btn : $("#query_btn"),
					$register_btn : $("#register_btn"),
					$clean_btn : $("#clean_btn")
				},

			};

			function initBoxInfoGrd() {
				domObj.grids.$boxInfoGrd.jqGrid({
					url : "",
					datatype : "local",
					mtype : "POST",
					height : 500,
					// width : $("#boxInfoGrdDiv").width() * 0.99,
					autowidth : true,
					shrinkToFit : true,
					resizable : true,
					loadonce : true,
					viewrecords : true,
					rownumbers : true,
					rowNum : 20,
					emptyrecords : true,
					//multiselect : true,
					colModel : [ {
						name : 'box_id',
						index : 'box_id',
						label : '卡匣代码',
						width : 180
					}, {
						name : 'box_stat',
						index : 'box_stat',
						label : '状态',
						width : 180
					}, {
						name : 'prd_qty',
						index : 'prd_qty',
						label : '玻璃数量',
						width : 180
					}, {
						name : 'wo_id',
						index : 'wo_id',
						label : '工单',
						width : 180
					}, 
					{
						name : 'so_id',
						index : 'so_id',
						label : '内部订单',
						width : 180
					}, {
						name : 'nx_ope_id_fk',
						index : 'nx_ope_id_fk',
						label : '站点信息',
						width : 180
					}, {
						name : 'tool_id_fk',
						index : 'tool_id_fk',
						label : '设备代码',
						width : 180
					}, {
						name : 'nx_path_id_fk',
						index : 'nx_path_id_fk',
						label : '工艺路线代码',
						width : 180
					},
                        {
                            name : 'nx_path_ver_fk',
                            index : 'nx_path_ver_fk',
                            label : '工艺路线版本',
                            width : 100
                        }],

					pager : "boxInfoPg",
					onSelectRow : function(rowId) {
						var selRowId, rowData, iary, woId, inObj, outObj, oary;
						rowData = domObj.grids.$boxInfoGrd.jqGrid("getRowData",
								rowId);
						SelectDom.setSelect(domObj.$woIdFkA, rowData.wo_id,
								rowData.wo_id);
						SelectDom.setSelect(domObj.$opeId ,
								rowData.nx_ope_id_fk, rowData.nx_ope_id_fk);
						domObj.$boxId.val(rowData.box_id);
						// unableFn();
					}
				});
			}
			;
			initBoxInfoGrd();
            //模态框拖动
            $(".modal-content").draggable();
            //表格自适应
            function resizeFnc(){
                var offsetBottom, divWidth;
                divWidth = $("#boxInfoGrdDiv").width();
                offsetBottom = $(window).height() - $("#boxInfoGrdDiv").offset().top;
                $("#boxInfoGrdDiv").height(offsetBottom * 0.99);
                $("#boxInfoGrd").setGridWidth(divWidth * 0.99);
                $("#boxInfoGrd").setGridHeight(offsetBottom * 0.99-51);

            };
            resizeFnc();
            $(window).resize(function() {
                resizeFnc();
            });
			//初始化下拉框
			var initSelectFnc = {
				//1.初始化内部订单
				initWoIdAFnc : function() {
					var inObj, outObj, i, tblCnt, oary, warp, option;

					var iary = {
						wo_stat : 'RLSE'
					}
					inObj = {
						trx_id : VAL.T_FDPAPLYWO,
						action_flg : 'Q',
						iary : [ iary ]
					};
					outObj = comTrxSubSendPostJson(inObj);
					if (outObj.rtn_code === VAL.NORMAL) {
                        SelectDom .addSelectArr(domObj.$woIdFkA, outObj.oary, "wo_id","wo_id","",true);
					}

				},
				
				initWoIdBFnc : function() {
					var inObj, outObj, i, tblCnt, oary, warp, option;

					var iary = {
						// wo_stat : 'WAIT'
					}
					inObj = {
						trx_id : VAL.T_FDPAPLYWO,
						action_flg : 'Q',
						iary : [ iary ]
					};
					outObj = comTrxSubSendPostJson(inObj);
					if (outObj.rtn_code === VAL.NORMAL) {
                        SelectDom.addSelectArr(domObj.$woIdFkB, outObj.oary, "wo_id","wo_id","",true);
					}

				},


				initOpeIdFnc : function() {
					var iary, inTrxObj, outObj, tbl_cnt, warp, option;
					
					inObj = {
							trx_id : VAL.T_FBPBISOPE,
							action_flg : "L",
							iary : [{}]
						};
                    outObj = comTrxSubSendPostJson(inObj);
					if (outObj.rtn_code == "0000000") {
                        SelectDom.addSelectArr(domObj.$opeId, outObj.oary, "ope_id","ope_id","",true);
					}
				}
			};

			initSelectFnc.initWoIdAFnc();
			initSelectFnc.initWoIdBFnc();
			initSelectFnc.initOpeIdFnc();

			var buttonFnc = {
				queryFnc : function() {
					var woIdFk, boxIdFk, opeId,iary, inObj, outObj;
					woIdFkA = domObj.$woIdFkA.val();
					boxIdFk = domObj.$boxId.val();
					opeId = domObj.$opeId.val();
                    var rowDatas = $("#boxInfoGrd").jqGrid("getRowData");
                    if(rowDatas.length>0){
                        for (var i = 0; i < rowDatas.length; i++) {
                            if (rowDatas[i].box_id.trim() === boxId.trim() ) {
                                $("#boxId").val("");
                                return false;
                            }
                        }
                    }

					iary = {
						wo_idA : woIdFkA,
						box_id : boxIdFk,
						ope_id : opeId
					};
					inObj = {
						trx_id : VAL.T_FSPWOCHG,
						action_flg : 'Q',
						iary : [ iary ]
					};
					outObj = comTrxSubSendPostJson(inObj);
					if (outObj.rtn_code === VAL.NORMAL) {
						setGridInfo(outObj.oary, "#boxInfoGrd", true);
					}

				},

				registerFnc : function() {
					var woIdFkB, id, inObj, inObj2,outObj,outObj2,rowData,iaryA,iary2;
					woIdFkB = domObj.$woIdFkB.val();
						
					//选中的卡匣
					id = domObj.grids.$boxInfoGrd.jqGrid('getGridParam',
							'selrow');
				
					if (!id) {
						showErrorDialog("", "请选择需要转工单的卡匣！");
						return false;
					}

					if (!woIdFkB) {
						showErrorDialog("", "转入工单不能为空,请选择");
						return false;
					}

					rowData = domObj.grids.$boxInfoGrd.jqGrid("getRowData",id);
					// var pathId = rowData.nx_path_id_fk;
					//
					// iaryA = {
					// 		path_id : pathId
					// };
					// inObj = {
					// 		trx_id : VAL.T_FBPBISPTH,
					// 		action_flg : 'Q',
					// 		iaryA : [iaryA]
					// };
					// outObj = comTrxSubSendPostJson(inObj);
					// if(outObj.rtn_code === VAL.NORMAL){
					// 	if(outObj.oaryA.length > 0){
					// 		if(outObj.oaryA[0].path_cate!='MAN'){
					// 			showErrorDialog("", "该卡匣不在主流程上，无法转工单");
					// 			return false;
					// 		}
					// 	}else{
					// 		showErrorDialog("", "该卡匣没有流程，无法转工单");
					// 		return false;
					// 	}
					// }
					//
					var woIdFkA = domObj.$woIdFkA.val();
					var opeId = domObj.$opeId.val();
					iary2 ={
							box_id : rowData.box_id,
							wo_idB: woIdFkB,
							wo_idA: woIdFkA,
							ope_id :opeId
					}
					
					inObj2 = {
						trx_id : VAL.T_FSPWOCHG,
						action_flg : 'R',
						iary : [iary2],
					};
					outObj2 = comTrxSubSendPostJson(inObj2);
					if (outObj2.rtn_code === VAL.NORMAL) {
						//更新选中的那行
						var selRowId = domObj.grids.$boxInfoGrd.jqGrid("getGridParam", "selrow");
						domObj.grids.$boxInfoGrd.jqGrid("setRowData", selRowId,outObj2.oary[0]);
						//setGridInfo(outObj2.oary, "#boxInfoGrd", true);
						showSuccessDialog("转工单成功！");
					}
				},

				cleanFnc : function() {
					initSelectFnc.initWoIdAFnc();
					initSelectFnc.initWoIdBFnc();
					initSelectFnc.initOpeIdFnc();
					enableFn();
					domObj.$boxId.val("");
					domObj.grids.$boxInfoGrd.jqGrid('clearGridData');
				}

			};

			domObj.buttons.$query_btn.bind('click', buttonFnc.queryFnc);
			domObj.buttons.$register_btn.bind('click', buttonFnc.registerFnc);
			domObj.buttons.$clean_btn.bind('click', buttonFnc.cleanFnc);

			domObj.$boxId.keydown(function(event) {
				var inObj, outObj, tblCnt, boxId, newRowId, iary;
				boxId = domObj.$boxId.val();

                var rowDatas = $("#boxInfoGrd").jqGrid("getRowData");
                if(rowDatas.length>0){
                    for (var i = 0; i < rowDatas.length; i++) {
                        if (rowDatas[i].box_id.trim() === boxId.trim() ) {
                            $("#boxId").val("");
                            return false;
                        }
                    }
                }

				iary = {
					box_id : boxId
				};
				if (event.keyCode == 13) {
					inObj = {
						trx_id : VAL.T_FSPWOCHG,
						action_flg : 'Q',
						iary : [ iary ]
					}, outObj = comTrxSubSendPostJson(inObj);
					if (outObj.rtn_code === VAL.NORMAL) {
						newRowId = getGridNewRowID("#boxInfoGrd");
						domObj.grids.$boxInfoGrd.jqGrid("addRowData", newRowId,
								outObj.oary[0]);
					}
				}
			});

			//将控件变成不可修改
			function unableFn() {
				domObj.$woIdFkA.attr(VAL.DISABLED_ATTR);
				domObj.$opeId.attr(VAL.DISABLED_ATTR);
				domObj.$boxId.attr(VAL.DISABLED_ATTR);
			}
			;

			//将控件变成都可修改
			function enableFn() {
				domObj.$woIdFkA.attr(VAL.ENABLED_ATTR);
				domObj.$opeId.attr(VAL.ENABLED_ATTR);
				domObj.$boxId.attr(VAL.ENABLED_ATTR);
			}
			;
			
			function initPage(){
				domObj.$boxId.val("");
				initSelectFnc.initWoIdAFnc();
				initSelectFnc.initWoIdBFnc();
				initSelectFnc.initOpeIdFnc();
				domObj.grids.$boxInfoGrd.jqGrid('clearGridData');
			};
			
			initPage();
            //表格自适应
            function resizeFnc(){
                var offsetBottom, divWidth;
                divWidth = $("#boxInfoGrdDiv").width();
                offsetBottom =  $(window).height() - $("#boxInfoGrdDiv").offset().top;
                $("#boxInfoGrdDiv").height(offsetBottom * 0.95);
                $("#boxInfoGrd").setGridWidth(divWidth * 0.99);
                $("#boxInfoGrd").setGridHeight(offsetBottom * 0.99 - 51);
            };
            resizeFnc();
            $(window).resize(function() {
                resizeFnc();
            });
		})